package com.yuyou.fn.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.*;
import com.yuyou.fn.educational.service.*;
import com.yuyou.fn.educational.vo.*;
import com.yuyou.fn.platform.entity.*;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.platform.service.*;
import com.yuyou.fn.platform.vo.SysResourceVo;
import com.yuyou.fn.platform.vo.SysUserDetailQueryVo;
import com.yuyou.fn.platform.vo.SysUserSaveOrUpdateVo;
import com.yuyou.fn.vo.AllinPayCusIdConfigVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by wens on 16-11-29.
 */
@Controller
@RequestMapping("/api/admin")
public class AdminSysApiController extends ApiBaseController {

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private ISysResourceService sysResourceService;

    @Resource
    private ISysRoleResourceService sysRoleResourceService;

    @Resource
    private ISysRoleService sysRoleService;

    @Resource
    private ISysUserRoleService sysUserRoleService;

    @Resource
    private IBusTeamService busTeamService;

    @Resource
    private IAreaTeamService areaTeamService;

    @Resource
    private IPeriodService periodService;

    @Resource
    private IStudentSchoolService studentSchoolService;

    @Resource
    private IClassRemunerationService classRemunerationService;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Resource
    private IRoomPriceService roomPriceService;

    @Resource
    private ISummerWinterDateService summerWinterDateService;

    @Resource
    private IContinueClassSettingService continueClassSettingService;

    @Resource
    private IPeriodGradeDateService periodGradeDateService;

    @Resource
    private ISysUserDetailService sysUserDetailService;

    @Resource
    private ICampusService campusService;

    @Resource
    private IRefundSettingService refundSettingService;

    @Resource
    private IBaseGradeService baseGradeService;

    @Resource
    private IBaseSubjectService baseSubjectService;

    @ApiOperation("系统用户列表")
    @RequestMapping(value = "/sysUserList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<SysUserDetail>> sysUserList(SysUserDetailQueryVo sysUserQueryVo, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "20") int pageSize, HttpServletRequest request) {
        DataPermission dataPermission = getDataPermission(request);
        Page<SysUserDetail> page = sysUserDetailService.findSysUserDetailByQueryForPage(sysUserQueryVo, dataPermission, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("系统用户详情")
    @RequestMapping(value = "/sysUserDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<SysUser> sysUserDetail(@RequestParam("userId") Long userId) {
        SysUser sysUser = sysUserService.findById(userId);
        return ReturnResult.success(sysUser);
    }

    @ApiOperation("重置为默认密码")
    @RequestMapping(value = "/resetDefaultPassword", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<SysUser> resetDefaultPassword(@RequestParam("userId") Long userId) {
        sysUserService.resetDefaultPassword(userId);
        return ReturnResult.success("ok");
    }

    @ApiOperation("删除系统用户")
    @RequestMapping(value = "/delSysUser", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> delSysUser(@RequestParam("userId") Long userId) {
        sysUserService.deleteUser(userId);
        return ReturnResult.success("OK");
    }

    @ApiOperation("保存更新系统用户")
    @RequestMapping(value = "/saveOrUpdateSysUser", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateSysUser(SysUserSaveOrUpdateVo sysUserSaveOrUpdateVo, HttpServletRequest request) {
        //sysUserSaveOrUpdateVo.setAreaTeamId(getUser(request).getAreaTeamId());
        sysUserService.saveOrUpdateSysUser(sysUserSaveOrUpdateVo);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("更新用户状态系统用户")
    @RequestMapping(value = "/updateSysUserStatus", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> updateSysUserStatus(@RequestParam("userId") Long userId, @RequestParam("status") Integer status) {
        sysUserService.updateSysUserStatus(userId, status);
        return ReturnResult.success("Ok");
    }


    @ApiOperation("系统资源列表")
    @RequestMapping(value = "/sysResourceList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<SysResourceVo>> sysResourceList() {
        List<SysResourceVo> allResource = sysResourceService.findAllResource();
        return ReturnResult.success(allResource);
    }

    @ApiOperation("角色资源列表")
    @RequestMapping(value = "/roleResourceList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<SysRoleResource>> userResourceList(@RequestParam("roleId") Long roleId) {
        List<SysRoleResource> sysUserResourceList = sysRoleResourceService.findRoleResourceByRoleId(roleId);
        return ReturnResult.success(sysUserResourceList);
    }

    @ApiOperation("保存角色资源")
    @RequestMapping(value = "/saveRoleResource", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveUserResource(@RequestParam("roleId") Long roleId, @RequestParam("resourceIds[]") List<Long> resourceIds) {
        sysRoleResourceService.saveOrUpdateRoleResource(resourceIds, roleId);
        return ReturnResult.success("OK");
    }

    @ApiOperation("角色列表")
    @RequestMapping(value = "/roleList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<SysRole>> roleList() {
        return ReturnResult.success(sysRoleService.findAll());
    }

    @ApiOperation("角色详情")
    @RequestMapping(value = "/roleDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<SysRole> roleDetail(@RequestParam(value = "roleId", required = false) Long roleId) {
        return ReturnResult.success(sysRoleService.findById(roleId));
    }

    @ApiOperation("保存角色")
    @RequestMapping(value = "/saveRole", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveRole(@RequestParam(value = "roleId", required = false) Long roleId, @RequestParam("roleName") String roleName) {
        sysRoleService.saveOrUpdateRole(roleId, roleName);
        return ReturnResult.success("OK");
    }

    @ApiOperation("删除角色")
    @RequestMapping(value = "/delRole", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> delRole(@RequestParam("roleId") Long roleId) {
        sysRoleService.deleteRole(roleId);
        return ReturnResult.success("OK");
    }

    @ApiOperation("角色用户列表")
    @RequestMapping(value = "/roleUserList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<SysUser>> roleUserList(@RequestParam("roleId") Long roleId) {
        List<Long> userIds = sysUserRoleService.findSysUserByRoleId(roleId);
        if (userIds.isEmpty()) {
            return ReturnResult.success(Collections.EMPTY_LIST);
        } else {
            return ReturnResult.success(sysUserService.findByIds(userIds));
        }
    }

    @ApiOperation("回收角色")
    @RequestMapping(value = "/recycleRole", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> recycleRole(@RequestParam("userId") Long userId, @RequestParam("roleId") Long roleId) {
        sysUserRoleService.deleteUserRole(userId, roleId);
        return ReturnResult.success("OK");
    }


    @ApiOperation("保存更新用户角色")
    @RequestMapping(value = "/saveUserRole", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveUserRole(@RequestParam("roleIds[]") List<Long> roleIds, @RequestParam("userId") Long userId) {
        sysUserRoleService.saveOrUpdateUserRole(roleIds, userId);
        return ReturnResult.success("OK");
    }

    @ApiOperation("用户角色列表")
    @RequestMapping(value = "/userRoleList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<SysUserRole>> userRoleList(@RequestParam("userId") Long userId) {
        List<SysUserRole> userRoleList = sysUserRoleService.findRoleByUserId(userId);
        return ReturnResult.success(userRoleList);
    }


    @ApiOperation("业务组列表")
    @RequestMapping(value = "/busTeamList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<BusTeam>> busTeamList(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "20") int pageSize, HttpServletRequest request) {
        DataPermission dataPermission = getDataPermission(request);
        Page<BusTeam> page = busTeamService.findBusTeamForManage(dataPermission, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("区域下所有业务组，不受权限控制")
    @RequestMapping(value = "/allBusTeamList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<BusTeam>> allBusTeamList(@RequestParam(value = "areaTeamId") Long areaTeamId, HttpServletRequest request) {
        List<BusTeam> busTeamList = busTeamService.findBusTeamByAreaTeamIds(Arrays.asList(areaTeamId));
        Collections.sort(busTeamList, new Comparator<BusTeam>() {
            @Override
            public int compare(BusTeam o1, BusTeam o2) {
                return  Utils.toPinyin(o1.getName().replace("誉优","")).toLowerCase().compareTo(Utils.toPinyin(o2.getName().replace("誉优","")).toLowerCase());
            }
        });

        return ReturnResult.success(busTeamList);
    }


    @ApiOperation("业务组详情")
    @RequestMapping(value = "/busTeamDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<BusTeam> busTeamDetail(@RequestParam("busTeamId") Long busTeamId) {
        BusTeam busTeam = busTeamService.findById(busTeamId);
        return ReturnResult.success(busTeam);
    }

    @ApiOperation("保存更新业务组")
    @RequestMapping(value = "/saveBusTeam", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveBusTeam(@RequestParam(value = "busTeamId", required = false) Long busTeamId, @RequestParam(value = "areaTeamId", required = false) Long areaTeamId, @RequestParam(value = "name", required = false) String name) {
        busTeamService.saveOrUpdateBusTeam(busTeamId, areaTeamId, name);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("删除业务组")
    @RequestMapping(value = "/delBusTeam", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> delBusTeam(@RequestParam(value = "busTeamId") Long busTeamId) {
        busTeamService.deleteBusTeam(busTeamId);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("区域组列表")
    @RequestMapping(value = "/areaTeamList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<AreaTeam>> areaTeamList(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "20") int pageSize, HttpServletRequest request) {
        DataPermission dataPermission = getDataPermission(request);
        Page<AreaTeam> page = areaTeamService.findAreaTeamForManage(dataPermission, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("区域组详情")
    @RequestMapping(value = "/areaTeamDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<AreaTeam> areaTeamDetail(@RequestParam("areaTeamId") Long areaTeamId) {
        AreaTeam areaTeam = areaTeamService.findById(areaTeamId);
        return ReturnResult.success(areaTeam);
    }

    @ApiOperation("区域更新业务组")
    @RequestMapping(value = "/saveAreaTeam", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveAreaTeam(@RequestParam(value = "areaTeamId", required = false) Long areaTeamId, @RequestParam(value = "name", required = false) String name, @RequestParam("province") String province, @RequestParam("city") String city) {
        areaTeamService.saveOrUpdateAreaTeam(areaTeamId, name, province, city);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("删除区域组")
    @RequestMapping(value = "/delAreaTeam", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> delAreaTeam(@RequestParam(value = "areaTeamId") Long areaTeamId) {
        areaTeamService.deleteAreaTeam(areaTeamId);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("期列表")
    @RequestMapping(value = "/periodList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<Period>> periodList(@RequestParam("areaTeamId") Long areaTeamId, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "20") int pageSize, HttpServletRequest request) {
        Page<Period> page = periodService.findPeriodForPage(areaTeamId, getDataPermission(request), pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("获取期数")
    @RequestMapping(value = "/beforePeriodList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<Period>> beforePeriodList(@RequestParam("areaTeamId") Long areaTeamId, @RequestParam(value = "periodId", required = false) Long periodId, HttpServletRequest request) {
        List<Period> list = periodService.findListBeforePeriodId(areaTeamId, periodId);
        return ReturnResult.success(list);
    }

    @ApiOperation("期详情")
    @RequestMapping(value = "/periodDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<PeriodVo> periodDetail(@RequestParam("periodId") Long periodId) {
        PeriodVo periodVo = periodService.findPeriodByPeriodId(periodId);
        return ReturnResult.success(periodVo);
    }

    @ApiOperation("获取区域期列表")
    @RequestMapping(value = "/periodListForAreaTeam", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<Period>> periodListForAreaTeam(@RequestParam("areaTeamId") Long areaTeamId) {
        if (areaTeamId == null) {
            return ReturnResult.success(Collections.EMPTY_LIST);
        }
        List<Period> list = periodService.findNearPeriod(areaTeamId, 10);
        return ReturnResult.success(list);
    }


    @ApiOperation("保存更新期")
    @RequestMapping(value = "/saveOrUpdatePeriod", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdatePeriod(PeriodSaveOrUpdateVo periodSaveOrUpdateVo, @RequestParam("segmentList") String segmentsJSONStr, HttpServletRequest request) {
        List<PeriodSegmentVo> segmentList = JSONObject.parseObject(segmentsJSONStr, new TypeReference<List<PeriodSegmentVo>>() {
        });
        periodService.saveOrUpdatePeriod(periodSaveOrUpdateVo, segmentList);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("设置期的年级开课日")
    @RequestMapping(value = "/savePeriodGradeDate", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> savePeriodGradeDate(@RequestParam("periodId") Long periodId, @RequestParam("periodGradeDateSaveList") String periodGradeDateSaveList, HttpServletRequest request) {
        List<PeriodGradeDateSaveVo> periodGradeDateSaveVoList = JSONObject.parseObject(periodGradeDateSaveList, new TypeReference<List<PeriodGradeDateSaveVo>>() {
        });
        periodGradeDateService.saveOrUpdate(periodId, periodGradeDateSaveVoList);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("获取期的年级开课日列表")
    @RequestMapping(value = "/periodGradeDateList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<PeriodGradeDate>> periodGradeDateList(@RequestParam("periodId") Long periodId, HttpServletRequest request) {
        List<PeriodGradeDate> periodGradeDateList = periodGradeDateService.findByPeriodId(periodId);
        return ReturnResult.success(periodGradeDateList);
    }


    @ApiOperation("设置当前期")
    @RequestMapping(value = "/updateCurrentPeriod", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> updateCurrentPeriod(@RequestParam("periodId") Long periodId) {
        periodService.updateCurrentPeriod(periodId);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("删除寒暑假")
    @RequestMapping(value = "/deleteSummerWinter", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> deleteSummerWinter(@RequestParam(value = "summerWinterDateId") Long summerWinterDateId, HttpServletRequest request) {
        summerWinterDateService.deleteById(summerWinterDateId);
        return ReturnResult.success("ok");
    }


    @ApiOperation("保存或更新寒暑假")
    @RequestMapping(value = "/saveOrUpdateSummerWinter", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateSummerWinter(@ModelAttribute SummerWinterDateVo summerWinterDateVo, HttpServletRequest request) {
        summerWinterDateService.saveOrUpdate(summerWinterDateVo);
        return ReturnResult.success("ok");
    }

    @ApiOperation("查询寒暑假")
    @RequestMapping(value = "/findSummerWinterByAreaId", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<SummerWinterDate> findSummerWinter(@RequestParam(value = "areaTeamId") Long areaTeamId, @RequestParam(value = "year") String year, HttpServletRequest request) {
        SummerWinterDate summerWinterDate = summerWinterDateService.findByAreaTeamIdAndYear(areaTeamId, year);
        return ReturnResult.success(summerWinterDate);
    }


    @ApiOperation("查询寒暑假")
    @RequestMapping(value = "/findSummerWinterById", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<SummerWinterDate> findSummerWinterById(@RequestParam(value = "summerWinterDateId") Long summerWinterDateId, HttpServletRequest request) {
        SummerWinterDate summerWinterDate = summerWinterDateService.findById(summerWinterDateId);
        return ReturnResult.success(summerWinterDate);
    }

    @ApiOperation("通过校区查询寒暑假")
    @RequestMapping(value = "/findSummerWinterByCampusId", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<RoomMessageVo> findSummerWinterByCampusId(@RequestParam(value = "roomId", required = false) Long roomId, @RequestParam(value = "campusId") Long campusId, @RequestParam(value = "year") String year, HttpServletRequest request) {
        SummerWinterDate summerWinterDate = summerWinterDateService.findByCampusIdAndYear(campusId, year);
        RoomMessageVo roomMessageVo = new RoomMessageVo();
        if (summerWinterDate != null) {
            BeanUtils.copyProperties(roomMessageVo, summerWinterDate);
        }
        if (roomId != null) {
            RoomPrice roomPrice = roomPriceService.findByRoomIdAndYear(roomId, year);
            if (roomPrice != null) {
                BeanUtils.copyProperties(roomMessageVo, roomPrice);
            }

        }
        return ReturnResult.success(roomMessageVo);
    }


    @ApiOperation("寒暑假列表")
    @RequestMapping(value = "/findSummerWinterList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<SummerWinterDate> findSummerWinterList(@RequestParam(value = "year", required = false) String year, HttpServletRequest request) {
        DataPermission dataPermission = getDataPermission(request);
        List<SummerWinterDate> summerWinterDateList = summerWinterDateService.findByYear(year, dataPermission.getAreaTeamIds());
        return ReturnResult.success(summerWinterDateList);
    }


    @ApiOperation("续读设置")
    @RequestMapping(value = "/saveOrUpdateContinueClassSetting", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateContinueClassSetting(@RequestBody ContinueClassSettingVo continueClassSettingVo) {
        continueClassSettingService.saveOrUpdateContinueClassSetting(continueClassSettingVo);
        return ReturnResult.success("OK");
    }

    @ApiOperation("获取某一期续读设置")
    @RequestMapping(value = "/continueClassSetting", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<ContinueClassSettingVo> continueClassSetting(@RequestParam("periodId") Long periodId) {
        ContinueClassSettingVo continueClassSetting = continueClassSettingService.findContinueClassSettingByPeriodId(periodId);
        return ReturnResult.success(continueClassSetting);
    }

    @ApiOperation("退费设置")
    @RequestMapping(value = "/saveOrUpadteRefundSetting", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpadteRefundSetting(@RequestBody RefundSettingVo refundSettingVo) {
        refundSettingService.saveOrUpdateRefundSetting(refundSettingVo);
        return ReturnResult.success("ok");
    }

    @ApiOperation("查询禁止退费业务组")
    @RequestMapping(value = "/queryRefundSettingBusteams", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<RefundsettingBusteamVo>> queryRefundSettingBusteams() {
        return ReturnResult.success(refundSettingService.queryRefundSettingBusteams());
    }

    @ApiOperation("查询退费设置")
    @RequestMapping(value = "/queryRefundSettings", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<RefundSettingVo> queryRefundSettingByPeriodId(@RequestParam("areaTeamId") Long areaTeamId, @RequestParam("periodId") Long periodId) {
        return ReturnResult.success(refundSettingService.queryRefundSettings(areaTeamId, periodId));
    }

    @ApiOperation("删除退费设置")
    @RequestMapping(value = "/deleteRefundSetting", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> deleteRefundSetting(@RequestParam("refundSettingId") Long refundSettingId) {
        refundSettingService.deleteRefundSetting(refundSettingId);
        return ReturnResult.success("ok");
    }

    @ApiOperation("生源学校列表")
    @RequestMapping(value = "/studentSchoolList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<StudentSchool>> studentSchoolList(@ModelAttribute StudentSchoolQueryVo studentSchoolQueryVo, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "20") int pageSize, HttpServletRequest request) {
        Page<StudentSchool> page = studentSchoolService.findStudentSchoolPageByQuery(studentSchoolQueryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("生源学校详情")
    @RequestMapping(value = "/studentSchoolDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<StudentSchool> studentSchoolDetail(@RequestParam("studentSchoolId") Long studentSchoolId) {
        StudentSchool studentSchool = studentSchoolService.findById(studentSchoolId);
        return ReturnResult.success(studentSchool);
    }


    @ApiOperation("保存更新生源学校")
    @RequestMapping(value = "/saveOrUpdateStudentSchool", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateStudentSchool(StudentSchoolSaveOrUpdateVo studentSchoolSaveOrUpdateVo, HttpServletRequest request) {
        studentSchoolService.saveOrUpdateStudentSchool(studentSchoolSaveOrUpdateVo);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("搜索生源学校")
    @RequestMapping(value = "/searchStudentSchool", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<StudentSchool>> searchStudentSchool(@RequestParam("query") String query, HttpServletRequest request) {
        List<StudentSchool> studentSchoolList = studentSchoolService.searchStudentSchool(query);
        return ReturnResult.success(studentSchoolList);
    }

    @ApiOperation("课酬列表")
    @RequestMapping(value = "/classRemunerationList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<ClassRemuneration>> classRemunerationList(ClassRemunerationQueryVo classRemunerationQueryVo, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "20") int pageSize, HttpServletRequest request) {

        DataPermission dataPermission = getDataPermission(request);

        Page<ClassRemuneration> page = classRemunerationService.findClassRemunerationForPage(classRemunerationQueryVo, dataPermission, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("课酬详情")
    @RequestMapping(value = "/classRemunerationDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<ClassRemuneration> classRemunerationDetail(@RequestParam("classRemunerationId") Long classRemunerationId) {
        ClassRemuneration classRemuneration = classRemunerationService.findById(classRemunerationId);
        return ReturnResult.success(classRemuneration);
    }


    @ApiOperation("保存更新课酬")
    @RequestMapping(value = "/saveOrUpdateClassRemuneration", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateClassRemuneration(ClassRemunerationSaveOrUpdateVo classRemunerationSaveOrUpdateVo, HttpServletRequest request) {
        classRemunerationService.saveOrUpdateClassRemuneration(classRemunerationSaveOrUpdateVo);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("删除课酬")
    @RequestMapping(value = "/delClassRemuneration", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateClassRemuneration(@RequestParam("classRemunerationId") Long classRemunerationId, HttpServletRequest request) {
        classRemunerationService.deleteById(classRemunerationId);
        return ReturnResult.success("Ok");
    }


    @ApiOperation("用户数据权限列表")
    @RequestMapping(value = "/dataPermissionList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<SysDataPermission>> userDataPermissionList(@RequestParam("userId") Long userId) {
        List<SysDataPermission> sysDataPermissionList = sysDataPermissionService.findDataPermission(userId);
        return ReturnResult.success(sysDataPermissionList);
    }

    @ApiOperation("保存数据权限")
    @RequestMapping(value = "/saveUserDataPermission", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveUserDataPermission(@RequestParam("userId") Long userId, @RequestParam("permissionJSON") String permissionStr) {
        List<SysDataPermission> sysDataPermissionList = JSONObject.parseObject(permissionStr, new TypeReference<List<SysDataPermission>>() {
        });
        sysDataPermissionService.saveDataPermission(userId, sysDataPermissionList);
        return ReturnResult.success("OK");
    }


    @ApiOperation("系统数据权限配置")
    @RequestMapping(value = "/sysDataPermissionConfig", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<JSONArray> sysDataPermissionConfig(HttpServletRequest request) {

        List<AreaTeam> areaTeamList = areaTeamService.findAllAreaTeam();
        List<Long> areaTeamIdList = BeanUtils.toList(areaTeamList, "areaTeamId");
        List<BusTeam> busTeamList = busTeamService.findBusTeamByAreaTeamIds(areaTeamIdList);

        Map<Long, List<BusTeam>> busTeamGroup = BeanUtils.toGroup(busTeamList, "areaTeamId");


        JSONArray config = new JSONArray();

        JSONArray areaTeamConfig = new JSONArray();
        JSONArray busTeamConfig = new JSONArray();
        JSONArray campusConfig = new JSONArray();
        JSONArray gradeConfig=new JSONArray();
        JSONArray subjectConfig=new JSONArray();

        JSONObject node1 = new JSONObject();
        node1.put("id", "-1");
        node1.put("name", "区域数据权限");
        node1.put("sub", areaTeamConfig);
        JSONObject node2 = new JSONObject();
        node2.put("id", "-2");
        node2.put("name", "业务组数据权限");
        node2.put("sub", busTeamConfig);
        JSONObject node3 = new JSONObject();
        node3.put("id", "-3");
        node3.put("name", "校区数据权限");
        node3.put("sub", campusConfig);

        JSONObject node4=new JSONObject();
        node4.put("id","-4");
        node4.put("name","年级数据权限");
        node4.put("sub",gradeConfig);

        JSONObject node5=new JSONObject();
        node5.put("id","-5");
        node5.put("name","科目数据权限");
        node5.put("sub",subjectConfig);


        config.add(node1);
        config.add(node2);
        config.add(node3);
        config.add(node4);
        config.add(node5);

        for (AreaTeam areaTeam : areaTeamList) {
            JSONObject node = new JSONObject();
            node.put("id", areaTeam.getAreaTeamId());
            node.put("name", areaTeam.getName());
            node.put("resourceType", "0");
            areaTeamConfig.add(node);
        }


        for (AreaTeam areaTeam : areaTeamList) {
            JSONObject pNode = new JSONObject();
            pNode.put("id", areaTeam.getAreaTeamId() + "1");
            pNode.put("name", areaTeam.getName());
            List<BusTeam> busTeamList1 = busTeamGroup.get(areaTeam.getAreaTeamId());
            if (busTeamList1 != null && !busTeamList1.isEmpty()) {
                JSONArray ret2 = new JSONArray();
                for (BusTeam busTeam : busTeamList1) {
                    JSONObject node = new JSONObject();
                    node.put("id", busTeam.getBusTeamId());
                    node.put("name", busTeam.getName());
                    node.put("resourceType", "1");
                    ret2.add(node);
                }
                pNode.put("sub", ret2);
            }
            busTeamConfig.add(pNode);
        }
        List<Campus> campusList = campusService.findByIdsAndStateAndAreaTeamIds(1, areaTeamIdList);
        Map<Long, List<Campus>> campusGroup = BeanUtils.toGroup(campusList, "areaTeamId");
        for (AreaTeam areaTeam : areaTeamList) {

            JSONObject pNode = new JSONObject();
            pNode.put("id", areaTeam.getAreaTeamId() + "2");
            pNode.put("name", areaTeam.getName());

            List<Campus> campusList1 = campusGroup.get(areaTeam.getAreaTeamId());
            if (campusList1 != null && !campusList1.isEmpty()) {
                JSONArray ret2 = new JSONArray();

                for (Campus campus : campusList1) {
                    JSONObject node = new JSONObject();
                    node.put("id", campus.getCampusId());
                    node.put("name", campus.getCampusName());
                    node.put("resourceType", "2");
                    ret2.add(node);
                }

                pNode.put("sub", ret2);
            }
            campusConfig.add(pNode);
        }
        List<BaseGrade> baseGradeList=baseGradeService.findAllOrderGrade();
        for (BaseGrade baseGrade : baseGradeList) {
            JSONObject node = new JSONObject();
            node.put("id", baseGrade.getGradeId());
            node.put("name", baseGrade.getGradeName());
            node.put("resourceType", "3");
            gradeConfig.add(node);
        }


        List<BaseSubject> baseSubjectList=baseSubjectService.findAllFromCache();
        for (BaseSubject baseSubject:baseSubjectList){
            JSONObject node = new JSONObject();
            node.put("id", baseSubject.getSubjectId());
            node.put("name", baseSubject.getSubjectName());
            node.put("resourceType", "4");
            subjectConfig.add(node);
        }



        return ReturnResult.success(config);
    }


    @ApiOperation("校区收费post机商户号设置列表")
    @RequestMapping(value = "/allinPayCusIdConfigList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<AllinPayCusIdConfigVo>> allinPayCusIdConfigList(CampusQueryVo campusQueryVo, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "20") int pageSize, HttpServletRequest request) {
        Page<Campus> page = campusService.findCampusByQueryForUse(campusQueryVo, getDataPermission(request), pageNo, pageSize);

        Page<AllinPayCusIdConfigVo> newPage = Utils.newPage(page);

        if (page.getRecords() != null) {
            HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
            Map<String, String> config = hashOperations.entries("campus:cusId:config");
            List<AllinPayCusIdConfigVo> list = new ArrayList<>(page.getRecords().size());
            for (Campus campus : page.getRecords()) {
                AllinPayCusIdConfigVo allinPayCusIdConfigVo = new AllinPayCusIdConfigVo();
                allinPayCusIdConfigVo.setCampus(campus);
                allinPayCusIdConfigVo.setCusId(config.get(String.valueOf(campus.getCampusId())));
                list.add(allinPayCusIdConfigVo);
            }
            newPage.setRecords(list);
        }

        return ReturnResult.success(newPage);
    }

    @ApiOperation("校区收费post机商户号设置列表")
    @RequestMapping(value = "/allinPayCusIdConfig", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<AllinPayCusIdConfigVo> allinPayCusIdConfig(@RequestParam("campusId") Long campusId) {
        Campus campus = campusService.findById(campusId);

        AllinPayCusIdConfigVo allinPayCusIdConfigVo = new AllinPayCusIdConfigVo();
        allinPayCusIdConfigVo.setCampus(campus);
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
        allinPayCusIdConfigVo.setCusId(hashOperations.get("campus:cusId:config", String.valueOf(campus.getCampusId())));

        return ReturnResult.success(allinPayCusIdConfigVo);
    }

    @ApiOperation("校区收费post机商户号设置列表")
    @RequestMapping(value = "/saveAllinPayCusIdConfig", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveAllinPayCusIdConfig(@RequestParam("campusId") Long campusId, @RequestParam("cusId") String cusId) {

        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
        hashOperations.put("campus:cusId:config", String.valueOf(campusId), cusId.trim());

        return ReturnResult.success("OK");
    }


}
